package com.example.movieratingbackend.controller;


import com.example.movieratingbackend.model.Movie;
import com.example.movieratingbackend.service.MovieService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/movies")
public class MovieController {
    private final MovieService movieService;

    public MovieController(MovieService movieService) {
        this.movieService = movieService;
    }

    @GetMapping("/getMovieList")
    public Flux<Movie> getMovies() {
        return movieService.getAllMovies();
    }


    @GetMapping("/getById/{id}")
    public Mono<Movie> getMovieDetailById(@PathVariable Long id) {
        return movieService.getMovieById(id);
    }


    @PostMapping("/rating/{id}")
    public Mono<ResponseEntity<Movie>> submitRating(@PathVariable Long id, @RequestBody int rating) {
        Mono<Movie> movieMono = movieService.submitRating(id, rating);
        return movieMono.map(ResponseEntity::ok).defaultIfEmpty(
                ResponseEntity.noContent().build()
        );
    }
}